package com.scores.controller;


import com.scores.pojo.Score;
import com.scores.pojo.Student;
import com.scores.pojo.ThreeS;
import com.scores.service.ScoreService;
import com.scores.utils.Results;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    ScoreService scoreService;

    @RequestMapping("/getAllScore")
    public Results<List<ThreeS>> getAllScoresByPage(String studentName, Integer page, Integer limit) {

        List<ThreeS> scoreList = scoreService.getAllScoresByPage(studentName, page, limit);

//        for (ThreeS score : scoreList) {
//            System.out.println(score);
//        }
        int count = scoreService.getScoreCount(studentName);
        if(!scoreList.isEmpty()) {
            return new Results<>("0", "Success", scoreList, count);

        } else {
            return new Results<>("1", "No data found", null, 0);
        }
    }

    @RequestMapping("/updateScore")
    public Results<String> updateScore(ThreeS threeS) {
//        System.out.println("Updating score: " + threeS);
        int resultCode = scoreService.updateScore(threeS);
        if (resultCode == 0) {
            return new Results<>("0", "更新成功", null, 0);
        } else if(resultCode == 2){
            return new Results<>("2", "该学生已存在该科目成绩", null, 0);
        }else {
            return new Results<>("1", "更新失败", null, 0);
        }
    }

    @RequestMapping("/addScore")
    public Results<String> addScore(ThreeS threeS) {
        Score score = new Score(threeS.getSubjectId(), threeS.getStudentNo(), threeS.getScore());
        int resultCode = scoreService.addScore(score);
        if (resultCode == 0) {
            return new Results<>("0", "添加成功");
        } else if(resultCode == 2){
            return new Results<>("2", "该学生已存在该科目成绩");
        }else {
            return new Results<>("1", "添加失败");
        }
    }

    @RequestMapping("/deleteScore")
    public Results<String> deleteScore(@RequestParam("scoreId") Integer scoreId) {
        int resultCode = scoreService.deleteScore(scoreId);
        if (resultCode == 0) {
            return new Results<>("0", "Delete successful");
        } else {
            return new Results<>("1", "Delete failed");
        }
    }

    @RequestMapping("/getTotalScore")
    public Results<List<ThreeS>> getTotalScore() {
        List<ThreeS> totalScore = scoreService.getTotalScore();
//        System.out.println("Total Score: " + totalScore);

        if (totalScore != null && !totalScore.isEmpty()) {
            return new Results<>("0", "Success", totalScore, 1);
        } else {
            return new Results<>("1", "No data found", null, 0);
        }
    }

    @RequestMapping("/getScoreByStudentNo")
    public Results<List<ThreeS>> getScoreByStudentNo(HttpSession session,Integer page, Integer limit) {
        Student student = (Student) session.getAttribute("loginUser");
        if (student == null) {
            return new Results<>("1", "Not logged in");
        } else {
            List<ThreeS> scoreList = scoreService.getScoreByStudentNo(student.getStudentNo(), page, limit);
//            System.out.println(scoreList);
            if (scoreList != null && !scoreList.isEmpty()) {
                return new Results<>("0", "Success", scoreList, scoreList.size());
            } else {
                return new Results<>("2", "No scores found", null, 0);
            }
        }

    }


}
